﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace ActiveReportsDemo.Viewers
{
    public partial class AdvanceExportViewer : DemoBaseUserControl
    {
        public AdvanceExportViewer()
        {
            InitializeComponent();

            dlgSave = new SaveFileDialog();
            dlgSave.AddExtension = true;
            dlgSave.Filter = ".pdf|*.pdf";
            dlgSave.RestoreDirectory = false;
            _exportButton.Enabled = false;
        }

        /// <summary>
        /// 显示报表
        /// </summary>
        /// <param name="name"></param>
        public override void ShowReport()
        {
            GrapeCity.ActiveReports.PageReport report1 = new GrapeCity.ActiveReports.PageReport(new System.IO.FileInfo(string.Format(@"Reports\{0}", CurrentReportName)));
            base.reportDesigner.Report = report1;
            document = new GrapeCity.ActiveReports.Document.PageDocument(report1);
            ReportViewer.LoadDocument(document);

            ReportWebBrowser.DocumentText = CommonFunction.getDescription(CurrentReportID);
        }

        SaveFileDialog dlgSave = null;
        GrapeCity.ActiveReports.Document.PageDocument document = null;

        private void radioSecure_CheckedChanged(object sender, EventArgs e)
        {
            bool bChecked = radioSecure.Checked;

            txUserPwd.Enabled = bChecked;
            checkCopy.Enabled = bChecked;
            checkSecureEdit.Enabled = bChecked;
            checkPrint.Enabled = bChecked;
        }

        private void radioDigitalSign_CheckedChanged(object sender, EventArgs e)
        {
            bool bChecked = radioDigitalSign.Checked;

            txContact.Enabled = bChecked;
            checkEdit.Enabled = bChecked;
            checkDate.Enabled = bChecked;
        }

        private void btnExport_Click(object sender, EventArgs e)
        {
            dlgSave.FileName = "";

            if (dlgSave.ShowDialog() == DialogResult.OK)
            {
                Application.DoEvents();

                GrapeCity.ActiveReports.Export.Pdf.Section.PdfExport pdfExport1 = new GrapeCity.ActiveReports.Export.Pdf.Section.PdfExport();

                if (radioSecure.Checked)
                {
                    pdfExport1.Security.Encrypt = true;
                    pdfExport1.Security.Use128Bit = true;
                    if (txUserPwd.Text.Length > 0)
                        pdfExport1.Security.UserPassword = txUserPwd.Text;

                    pdfExport1.Security.Permissions = GrapeCity.ActiveReports.Export.Pdf.Section.PdfPermissions.None;
                    if (checkCopy.Checked)
                        pdfExport1.Security.Permissions |= GrapeCity.ActiveReports.Export.Pdf.Section.PdfPermissions.AllowCopy;

                    if (checkSecureEdit.Checked)
                    {
                        pdfExport1.Security.Permissions |= GrapeCity.ActiveReports.Export.Pdf.Section.PdfPermissions.AllowFillIn;
                        pdfExport1.Security.Permissions |= GrapeCity.ActiveReports.Export.Pdf.Section.PdfPermissions.AllowModifyAnnotations;
                        pdfExport1.Security.Permissions |= GrapeCity.ActiveReports.Export.Pdf.Section.PdfPermissions.AllowModifyContents;
                    }

                    if (checkPrint.Checked)
                        pdfExport1.Security.Permissions |= GrapeCity.ActiveReports.Export.Pdf.Section.PdfPermissions.AllowPrint;
                }
                else if (radioDigitalSign.Checked)
                {
                    // ImageText signature.
                    pdfExport1.Signature.VisibilityType = GrapeCity.ActiveReports.Export.Pdf.Section.Signing.VisibilityType.ImageText;

                    // Bounds (Container of Text & Image).
                    pdfExport1.Signature.Stamp.Bounds = new RectangleF(0, 0, 4, 1);

                    // Text area.
                    pdfExport1.Signature.Stamp.TextAlignment = GrapeCity.ActiveReports.Export.Pdf.Section.Signing.Alignment.Left;
                    pdfExport1.Signature.Stamp.Font = new Font("Comic Sans MS", 8, FontStyle.Regular);
                    // Note: Specify (x, y) in relative coordinate from Bounds top-left.
                    pdfExport1.Signature.Stamp.TextRectangle = new RectangleF(1, 0, 3, 1);

                    // Image area.
                    pdfExport1.Signature.Stamp.Image = Image.FromFile(Application.StartupPath + "\\Resources\\Grapecity_powertools_bg.png");
                    pdfExport1.Signature.Stamp.ImageAlignment = GrapeCity.ActiveReports.Export.Pdf.Section.Signing.Alignment.Center;
                    // Note: Specify (x, y) in relative coordinate from Bounds top-left.
                    pdfExport1.Signature.Stamp.ImageRectangle = new RectangleF(0, 0, 1, 1);

                    // Set certificate & password.
                    pdfExport1.Signature.Certificate = new System.Security.Cryptography.X509Certificates.X509Certificate2(Application.StartupPath + "\\Resources\\ActiveReports6.pfx", "123456");

                    // set the certifiation level
                    if (checkEdit.Checked)
                        pdfExport1.Signature.CertificationLevel = GrapeCity.ActiveReports.Export.Pdf.Section.Signing.CertificationLevel.FormFillingAnnotations;
                    else
                        pdfExport1.Signature.CertificationLevel = GrapeCity.ActiveReports.Export.Pdf.Section.Signing.CertificationLevel.NoChangesAllowed;

                    //Signature items.
                    pdfExport1.Signature.Contact = new GrapeCity.ActiveReports.Export.Pdf.Section.Signing.SignatureField<string>(txContact.Text, true);
                    if (checkDate.Checked)
                        pdfExport1.Signature.SignDate = new GrapeCity.ActiveReports.Export.Pdf.Section.Signing.SignatureField<System.DateTime>(System.DateTime.Now, true);
                }

                try
                {
                    pdfExport1.Export(document, dlgSave.FileName);

                    MessageBox.Show("文件已经成功导出！", "", MessageBoxButtons.OK, MessageBoxIcon.Information);                 

                }
                catch (Exception)
                {
                    MessageBox.Show("导出文件错误请检查相关设置！", "", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
                finally
                {
                    Application.DoEvents();
                }
            }
        }
    }
}
